<?php
use Managers\Auto,
Managers\Search,
Managers\Ad;
/**
 * Controller description
 * @since   7 мая 2010
 * @author diotheos
 */
class TovarController extends Zend_Controller_Action
{
    /**
     * @var Zend_Cache_Core
     */
    private $_cache;


    public function init()
    {

        $this->_helper->layout()->setLayout('index-layout');

    }

    public function indexAction()
    {
        $user_manager = new Managers\Tovar();
        $usertov = $user_manager->getTovarsByUserId('2');
        print_r($usertov);
    }

    public function viewAction()
    {
        $id = $this->_getParam('id');
        $tovar_manager = new Managers\Tovar();

        $connectManager = new \Managers\Connects();

        $tags = $connectManager->getTags($id);

        $this->view->tovar = $tovar_manager->GetTovarById($id);

        $this->view->tags = $tags;

    }

    public function addAction()
    {

        $name = $this->_getParam('name');
        $description = $this->_getParam('description');

        $tovar_manager = new Managers\Tovar();

        $connectManager = new \Managers\Connects();

        $ImageManager = new \Managers\Images();

        if ($this->_request->isPost()) {

            $tags = $this->_getParam('item-tags');
            //            if(count($tags) == 0) {
            //                die("Nety tagov");
            //            }
            $tovar = $tovar_manager->AddTovar($name, $description);
            $new_id = $tovar->getId();
            $connectManager->update($tovar->getId(), $tags);

            $this->view->tovar = $tovar;
            $this->view->tags = $connectManager->getTags($tovar->getId());

            if (file_exists($_FILES['Filedata']['tmp_name'])) {
                //            move_uploaded_file($_FILES['Filedata']['name'], '/tmp/1.jpg');
                Lib_Log::toFile('okkk', 'uploading', LOG_ERR);

                $sourceFile = $_FILES['Filedata']['tmp_name'];
                // die($sourceFile);
                //$tovar_image = new Managers\Tovar();
                //$id = $tovar_image->AddImage($sourceFile);

                $imageManager = new Managers\Images();

                $newurl = $imageManager->resizeImage($sourceFile, 100, 100, $new_id);

                $tovar_manager->updateImage($new_id, $newurl);
                $this->_redirect('/index/view/id/' . $new_id);
            }

            //   $this->view->image = $ImageManager->resizeImage(1,1,1,1);
        }

        // $this->renderScript('tovar/view.phtml');
    }

    public function resizeAction()
    {
        //        Lib_Log::toFile(print_r($_FILES['Filedata']['name'], true),'uploading', LOG_ERR);
        Lib_Log::toFile('okkk1111', 'uploading', LOG_ERR);
        Lib_Log::toFile($_FILES['Filedata']['name'], 'uploading', LOG_ERR);
        if (file_exists($_FILES['Filedata']['tmp_name'])) {
            //            move_uploaded_file($_FILES['Filedata']['name'], '/tmp/1.jpg');
            Lib_Log::toFile('okkk', 'uploading', LOG_ERR);

            $sourceFile = $_FILES['Filedata']['tmp_name'];
            // die($sourceFile);
            $tovar_image = new Managers\Tovar();
            $id = $tovar_image->AddImage($sourceFile);

            $imageManager = new Managers\Images();

            $newurl = $imageManager->resizeImage($sourceFile, 100, 100, $id);

        }


        //       Lib_Log::toFile(print_r($newurl, true),'uploading', LOG_ERR);
        //       die('1');
    }

    /**
     * Обновление записи о товаре.
     * @return void
     */
    public function updateAction()
    {
        //Обновление только Тэгов.
        $connectsManager = new \Managers\Connects();
        $tags = $this->_getParam('my-box-input');
        while (strpos($tags, '  ') !== false)
        {
            $tags = str_replace('  ', ' ', $tags);
        }
        ;
        $tags_array = explode(" ", trim($tags), 5);
        $id = $this->_getParam('id');
        $tags = $this->_getParam('item-tags');
     

        $adden_tags_id = $connectsManager->addTag($tags_array);
        
        foreach ($tags as $one_tag) {
            $var = null;
            foreach ($adden_tags_id as $one_adden_tag) {
                if ($one_tag == $one_adden_tag) {
                    $var = 1;
                }
            }
            if (!$var) {
                $adden_tags_id[] = $one_tag;

            }


        }


        /**
         * Обновляем список тэгов.
         */
        $connectsManager->update($id, $adden_tags_id);
        $this->view->usr=
        $this->_redirect("http://localhost/account/edit/id/".$id);

    }


}